#include "main.h"
#include "def.h"
#include "sync.h"
#include "util.h"
#include "gui.h"
#include "net.h"

#include <maya/MGlobal.h>
#include <maya/MFnDagNode.h>
#include <maya/MFnDagNode.h>
#include <maya/MFnMesh.h>
#include <maya/MItDependencyNodes.h>
#include <maya/MDagPath.h>
#include <maya/MSelectionList.h>
#include <maya/MEventMessage.h>
#include <maya/MCallbackIdArray.h>
#include <time.h>

#pragma message ("remove when VTRY/VCATCH refactored")
#include "..\Global\globaldef.h"

#pragma message ("todo reorganize 1")
#include "..\CommLib\PacketConven.h"

void VSyncViewer::AddNodeCallback(MObject &newObject, void* clientData) {

	if(newObject.hasFn(MFn::kMesh)) {
		AddMeshCallback(newObject);
	}
	else
	if(newObject.hasFn(MFn::kTransform)) {
		AddTransformCallback(newObject);
	}
	else
	if(newObject.hasFn(MFn::kMidModifier)) {
		AddUVCallback(newObject);
	}
	else
	if(newObject.hasFn(MFn::kLight)) {
		AddLightCallback(newObject);
	}
	else
	if(newObject.hasFn(MFn::kTexture2d) || newObject.hasFn(MFn::kLayeredTexture)) {
		AddSamplerCallback(newObject);
	}
	else {
		
		/*MessageBox(0,newObject.apiTypeStr(),0,0);*/
	}
}

//void VSyncViewer::AddMaterialAssignmentCallback(MObject material) {
//
//	if(material.apiType() == MFn::kShadingEngine) {
//
//		MStatus result;
//		MCallbackId samplerCallback = MNodeMessage::addAttributeChangedCallback(material, 
//			VSyncViewer::CallbackWrappers::OnChangeMeshMaterial, 0, &result);
//		
//		if(MAYA_FAIL(result))
//			return;
//		
//		VSyncViewer::g_RegisteredCallbacks.append(samplerCallback);
//	}
//}

void VSyncViewer::AddSamplerCallback(MObject material) {

	if(VUtil::IsTextureSupported(material) || material.hasFn(MFn::kLayeredTexture)) {

		MStatus result;
		MCallbackId samplerCallback = MNodeMessage::addAttributeChangedCallback(material, 
			VSyncViewer::CallbackWrappers::OnChangeSampler, 0, &result);
		
		if(MAYA_FAIL(result))
			return;
		
		VSyncViewer::g_RegisteredCallbacks.append(samplerCallback);
	}
}

void VSyncViewer::AddTransformCallback(MObject transform) {

	if(VUtil::IsTransform(transform)) {

		MStatus result;
		MCallbackId transformCallback = MNodeMessage::addAttributeChangedCallback(transform, 
			VSyncViewer::CallbackWrappers::OnChangeTransform, 0, &result);
		
		if(MAYA_FAIL(result))
			return;
		
		VSyncViewer::g_RegisteredCallbacks.append(transformCallback);
	}
}

void VSyncViewer::ReleaseRegisteredCallbacks() {

	MMessage::removeCallbacks(VSyncViewer::g_RegisteredCallbacks);
	VSyncViewer::g_RegisteredCallbacks.clear();
}

void VSyncViewer::AddMeshCallback(MObject mesh) {

	if(VUtil::IsMesh(mesh)) {

		MStatus result;
		MCallbackId meshCallback = MNodeMessage::addAttributeChangedCallback(mesh, 
			VSyncViewer::CallbackWrappers::OnChangeMesh, 0, &result);
		
		if(MAYA_FAIL(result))
			return;
		
		VSyncViewer::g_RegisteredCallbacks.append(meshCallback);
	}
}

void VSyncViewer::AddUVCallback(MObject mesh) {

	if(mesh.hasFn(MFn::kMidModifier)) {

		MStatus result;
		MCallbackId meshCallback = MNodeMessage::addAttributeChangedCallback(mesh, 
			VSyncViewer::CallbackWrappers::OnChangeUV, 0, &result);
		
		if(MAYA_FAIL(result))
			return;
		
		VSyncViewer::g_RegisteredCallbacks.append(meshCallback);
	}
}

void VSyncViewer::AddLightCallback(MObject light) {

	if(VUtil::IsLight(light)) {

		MStatus result;
		MCallbackId lightCallback = MNodeMessage::addAttributeChangedCallback(light, 
			VSyncViewer::CallbackWrappers::OnChangeLight, 0, &result);
		
		if(MAYA_FAIL(result))
			return;
		
		VSyncViewer::g_RegisteredCallbacks.append(lightCallback);
	}
}

bool VSyncViewer::InitSelectedAttributes(MObject& target) {

	MString meshName;
	if(!VUtil::GetObjectName(target, meshName))
		return false;

	MString command = MString("InitalizeObjectAttributes(\"" + meshName + "\");");
	MAYA_RETURN_IF_FAIL(MGlobal::executeCommand(command, false, false), false);
	return true;
}